import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import User from '../components/user/user.vue'
import Welcome from '../components/welcome.vue'
import Rights from '../components/power/rights.vue'
import Roles from '../components/power/roles.vue'


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  { path: '/home',
    component: Home,
    redirect: '/welcome',
    children:[
      {path: '/welcome', component: Welcome},
      {path: '/users', component: User},
	  {path: '/rights', component: Rights},
	  {path: '/roles', component: Roles}
    ]
  }
]

const router = new VueRouter({
  routes
})
//路由导航守卫
//对所有请求进行拦截，判断一下浏览器上是否有token,有token才能放行，不然直接跳转到登录页
router.beforeEach( (to,from,next) =>{
  //to 将要访问的url
  //from 当前的url
  //next() 放行  next('/login')跳转到登录页
  if(to.path == '/login') return next();
  //获得token
  let token = window.sessionStorage.getItem('token');
  //如果没token，直接跳转到登录页
  if(!token) return next('/login');
  next();
})


export default router
